//
//  KKPageItemsList.m
//  Butler
//
//  Created by liaozhenming on 16/7/1.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKPageItemsList.h"

@implementation KKPageItemsList

- (instancetype)initWithItems:(NSArray*)items {
    self = [super init];
    if (self) {
        if (items) {
            _limit = 8;
            self.items = [NSMutableArray arrayWithArray: items];
            
            _totalPage = items.count / _limit;
            if (items.count % _limit > 0) {
                _totalPage++;
            }
        }
    }
    return self;
}

- (NSArray*)kk_goodsItemWithPageIndex:(NSInteger)index {
    
    if (_limit == 0) {
        _limit = 8;
    }
    
    NSInteger count = self.items.count;
    NSInteger totalPage = count / _limit;
    
    if (totalPage < index) {
        return nil;
    }
    
    NSInteger startIndex = _limit *index;
    if (count % _limit == 0) {
        return totalPage == index ? nil : [_items subarrayWithRange: NSMakeRange(startIndex, _limit)];
    }
    else {
        if (totalPage == index) {
            return [_items subarrayWithRange: NSMakeRange(startIndex, _items.count - startIndex)];
        }
        else {
            return [_items subarrayWithRange: NSMakeRange(startIndex, _limit)];
        }
    }
    return nil;
}

@end
